package com.item.read.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;

import java.io.IOException;
import java.net.URI;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.time.Duration;

public class HttpUtils {

    private static HttpClient client = null;

    static{
        client = HttpClient.newBuilder()
                .version(HttpClient.Version.HTTP_2)
                .followRedirects(HttpClient.Redirect.NORMAL)
                .connectTimeout(Duration.ofSeconds(6))
                .build();
    }
    public static String get(String url){
        return sendHttpOfString("GET",url);
    }
    public static String post(String url){
        return sendHttpOfString("POST",url);
    }
    public static String get(String url,String param){
        return sendHttpOfString("GET",url,param);
    }
    public static String post(String url,String param){
        return sendHttpOfString("POST",url,param);
    }
    public static String sendHttpOfString(String method,String url,String param){
        HttpRequest request = HttpRequest.newBuilder()
                .version(HttpClient.Version.HTTP_2)
                .setHeader("Content-Type","application/json; charset=utf-8")
                .method(method, HttpRequest.BodyPublishers.ofString(param))
                .timeout(Duration.ofSeconds(2))
                .uri(URI.create(url))
                .build();

        try {
            return client.send(request,HttpResponse.BodyHandlers.ofString(Charset.forName("UTF-8"))).body();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sendHttpOfString(String method,String url){
        HttpRequest request = HttpRequest.newBuilder()
                .version(HttpClient.Version.HTTP_2)
                .method(method, HttpRequest.BodyPublishers.noBody())
                .timeout(Duration.ofSeconds(4))
                .uri(URI.create(url))
                .build();

        try {
            return client.send(request,HttpResponse.BodyHandlers.ofString(Charset.forName("UTF-8"))).body();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        var sourceCode = "https://shuyuan.miaogongzi.net/shuyuan/1630342495.json";
        String content = sendHttpOfString("GET",sourceCode);
        //System.out.println(new String(content.getBytes(StandardCharsets.UTF_8),StandardCharsets.UTF_8));
        JSONArray jsonArray = JSONArray.parseArray(content);

        for(int i=0;i<jsonArray.size();i++){
            String bookSourceUrl = jsonArray.getJSONObject(i).getString("bookSourceUrl");

            String searchUrlStr = jsonArray.getJSONObject(i).getString("searchUrl");
            if(searchUrlStr.contains("POST") || searchUrlStr.contains("post")){
//            int index = searchUrlStr.indexOf(",");
//            String serach = searchUrlStr.substring(0,index);
//            JSONObject param = JSONObject.parseObject(searchUrlStr.substring(index+1));
//
//            String method = param.getString("method");
//            String body   = param.getString("body")
//                    .replace("{{key}}",URLEncoder.encode("剑","utf-8"))
//                    .replace("{{page}}","20")
//                    ;
//            bookSourceUrl+= body;
            }else{
//                String body   = searchUrlStr
//                        .replace("{{key}}",URLEncoder.encode(new String("剑",""),"utf-8"))
//                        .replace("{{page}}","20")
//                        ;
//                bookSourceUrl+=body;
//                System.out.println( bookSourceUrl );
//                System.out.println( get(bookSourceUrl) );
            }
        }






       // URLDecoder.decode()
//        request = HttpRequest.newBuilder()
//                .version(HttpClient.Version.HTTP_1_1)
//                .method("GET", HttpRequest.BodyPublishers.noBody())
//                .timeout(Duration.ofSeconds(2))
//                .uri(URI.create(String.format(jsonArray.getJSONObject(0).getString("searchUrl"),"剑")))
//                .build();
//        System.out.println(client.sendAsync(request,HttpResponse.BodyHandlers.ofString()));
    }
}
